﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace BS_Gimnasio.General
{
    public static class cls_Adaptador
    {
        #region Metodo

        public static DataTable ToDataTable<t>(IList<t> list)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(t));
            DataTable dt = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                dt.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            }

            object[] values = new object[props.Count];
            foreach (t item in list)
            {
                for (int i = 0; i < values.Length; i++)
                    values[i] = props[i].GetValue(item) ?? DBNull.Value;
                dt.Rows.Add(values);
            }

            return dt;
        }

        #endregion

    }
}
